//createUserList:此函数执行会返回一个数组，数组里面包含两个用户信息
function createUserList() {
    return [
        {
            userId: 1,
            avatar:
                'https://c-ssl.duitang.com/uploads/blog/202207/18/20220718162721_87f57.jpg',
            username: 'admin',
            password: '123456',
            desc: '平台管理员',
            roles: ['平台管理员'],
            permissions: ['admin'], // 添加权限字段
            buttons: ['cuser.detail'],
            routes: ['home'],
            token: 'Admin Token',
        },
        {
            userId: 2,
            avatar:
                'https://c-ssl.duitang.com/uploads/blog/202207/18/20220718162721_87f57.jpg',
            username: 'system',
            password: '123456',
            desc: '系统管理员',
            roles: ['系统管理员'],
            permissions: [], // 普通用户无权限
            buttons: ['cuser.detail', 'cuser.user'],
            routes: ['home'],
            token: 'System Token',
        },
    ]
}

export default [
    // 用户登录接口
    {
        url: '/api/user/login',
        method: 'post',
        response: ({ body }) => {
            const { username, password } = body;
            const checkUser = createUserList().find(
                (item) => item.username === username && item.password === password,
            )
            if (!checkUser) {
                return { code: 201, data: { message: '账号或者密码不正确' } }
            }
            const { token } = checkUser
            return { code: 200, data: { token } }
        },
    },
    // 用户注册接口
    {
        url: '/api/register',
        method: 'post',
        response: ({ body }) => {
            const { username, password } = body
            if (username && password) {
                return {
                    code: 200,
                    message: '注册成功',
                    data: {
                        username,
                        token: 'mock-token'
                    }
                }
            }
            return {
                code: 400,
                message: '注册失败，请检查输入'
            }
        }
    },
    // 获取用户信息
    {
        url: '/api/user/info',
        method: 'get',
        response: (request) => {
            const token = request.headers.token;
            const checkUser = createUserList().find((item) => item.token === token)
            if (!checkUser) {
                return { code: 201, data: { message: '获取用户信息失败' } }
            }
            return { 
                code: 200, 
                data: {
                    name: checkUser.username,
                    avatar: checkUser.avatar,
                    permissions: checkUser.permissions || []
                } 
            }
        },
    },
]
